﻿using NUnit.Framework;
using ScrappyDB.Attributes;
using ScrappyDB.BaseClasses;

namespace ScrappyDB.IntegrationTests.Core
{
    [TestFixture]
    public class DBLoad
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [TestFixtureSetUp]
        public  void Init()
        {
            //test data - create 100 test records
            var a = new SdbCollection<TestEntity>();
            for (int x = 1; x <= 100; x++)
            {
                a.Add(new TestEntity() { Id = x.ToString(), TestString = "a" + x.ToString() });
            }

            var db = new Db();
            a.SaveChanges();
        }
        
        //Use ClassCleanup to run code after all tests in a class have run
        [TestFixtureTearDown]
        public static void MyClassCleanup()
        {
            var db = new Db();
            var a = db.Query<TestEntity>(null, true);
            db.Delete<TestEntity>(a);
        }
        
        //Use TestInitialize to run code before running each test
        //[SetUp]
        //public void MyTestInitialize()
        //{
        //}
        //

        //Use TestCleanup to run code after each test has run
        [TearDown]
        public void MyTestCleanup()
        {
        }
        
        #endregion




        [Test]
        public void LoadWithListTest()
        {
            Db target = new Db();

            //test
            object[] id = new object[] { "2", "3", "4" };
            var actual = target.Find<TestEntity>(id);
            Assert.AreEqual(3, actual.Count);

            object[] id2 = new object[] { "1", "2", "3", "4" };
            var actual2 = target.Find<TestEntity>(id2);
            Assert.AreEqual(4, actual2.Count);

            object[] id3 = new object[] { "4" };
            var actual3 = target.Find<TestEntity>(id3);
            Assert.AreEqual(1, actual3.Count);
        }

        [Test]
        public void LoadWithListExactly19()
        {
            int count = 19;

            SdbCollection<TestEntity> actual = GetSubset(count);

            Assert.AreEqual(count, actual.Count);
        }

        [Test]
        public void LoadWithList100()
        {
            int count = 100;

            SdbCollection<TestEntity> actual = GetSubset(count);

            Assert.AreEqual(count, actual.Count);
        }

        private SdbCollection<TestEntity> GetSubset(int count)
        {
            Db target = new Db();

            //test
            var id = new object[count];
            for (int x = 1; x <= count; x++)
            {
                id[x-1]=(x.ToString());
            }

            return target.Find<TestEntity>(true,id);
        }

        [DomainName("ScrappyDB_UnitTests"), EntityName("DBLoad_TestEntity")]
        private class TestEntity 
        {
            public string Id { get; set; }
            public string TestString { get; set; }
        }
    }
}
